package com.imooc.springboot.dubbo.nilinili.provider.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.imooc.springboot.dubbo.nilinili.entity.dto.LoginUserDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.UserDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.editUserDTO;
import com.imooc.springboot.dubbo.nilinili.provider.entity.po.UserPO;
import com.imooc.springboot.dubbo.nilinili.provider.entity.po.editUserPO;
import com.imooc.springboot.dubbo.nilinili.provider.mapper.UserMapper;
import com.imooc.springboot.dubbo.nilinili.provider.utils.JwtToUserId;
import com.imooc.springboot.dubbo.nilinili.provider.utils.JwtUtil;
import com.imooc.springboot.dubbo.nilinili.provider.utils.RedisCache;
import com.imooc.springboot.dubbo.nilinili.res.CodeEnum;
import com.imooc.springboot.dubbo.nilinili.res.ResultFactory;
import com.imooc.springboot.dubbo.nilinili.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;


@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {
    @Autowired
    RedisCache redisCache;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String userLogin(LoginUserDTO loginUserDTO) {
        UserPO userPO = userMapper.userLogin(loginUserDTO);
        if (userPO == null){
            return ResultFactory.createResult(CodeEnum.FAIL,"登录失败");
        }
        String jwt = JwtUtil.createJWT(userPO.getUserId());
        UserDTO userDTO = UserDTO.builder().userId(userPO.getUserId()).token(jwt).build();
        // 存储token至redis
        redisCache.setCacheObject("login:" + userPO.getUserId(), jwt, 7, TimeUnit.DAYS);
        return ResultFactory.createResult(CodeEnum.SUCCESS, "登录成功",userDTO);
    }


    @Override
    public String userLogout(String token) {
        // 解析用户id
        String user_id = JwtToUserId.Jwt2UserId(token);
        redisCache.deleteObject("login:" + user_id);
        return ResultFactory.createResult(CodeEnum.SUCCESS, "退出成功");
    }

    @Override
    public String getUserInfo(String token) {
        // 解析用户id
        String user_id = JwtToUserId.Jwt2UserId(token);
        UserPO userPO = userMapper.getUserInfo(user_id);
        if (userPO == null){
            return ResultFactory.createResult(CodeEnum.FAIL,"获取用户信息失败");
        }
        UserDTO userDTO = UserDTO.builder().userId(userPO.getUserId()).nick(userPO.getNick()).username(userPO.getUsername()).signature(userPO.getSignature()).photoCounts(userPO.getPhotoCounts()).collectionCounts(userPO.getCollectionCounts()).fanCounts(userPO.getFanCounts()).followCounts(userPO.getFollowCounts()).approvalCounts(userPO.getApprovalCounts()).userAvatar(userPO.getUserAvatar()).build();
        return ResultFactory.createResult(CodeEnum.SUCCESS, "获取用户信息成功",userDTO);
    }

    @Override
    @Transactional
    public String userRegister(UserDTO userDTO) {
        if (userMapper.getUsernameCount(userDTO.getUsername()) > 0){
            return ResultFactory.createResult(CodeEnum.FAIL,"用户名已存在");
        }
        UserPO userPO = UserPO.builder().userId(userDTO.getUserId()).username(userDTO.getUsername()).password(userDTO.getPassword()).nick(userDTO.getNick()).signature(userDTO.getSignature()).userAvatar(userDTO.getUserAvatar()).build();
        int i = userMapper.userRegister(userPO);
        if (i == 0){
            return ResultFactory.createResult(CodeEnum.FAIL,"注册失败");
        }
        return ResultFactory.createResult(CodeEnum.SUCCESS, "注册成功");
    }

    @Override
    public String checkLogin(String token) {
        // 解析用户id
        String user_id = JwtToUserId.Jwt2UserId(token);
        String jwt = redisCache.getCacheObject("login:" + user_id);
        if (jwt == null){
            return ResultFactory.createResult(CodeEnum.FAIL,"未登录");
        }
        return ResultFactory.createResult(CodeEnum.SUCCESS, "已登录");
    }


    @Override
    @Transactional
    public String editUser(editUserDTO  editUserDTO) {
        String user_id = JwtToUserId.Jwt2UserId(editUserDTO.getToken());
        editUserPO editUser = editUserPO.builder().userId(user_id).nick(editUserDTO.getNick()).signature(editUserDTO.getSignature()).userAvatar(editUserDTO.getUserAvatar()).build();
        int i = userMapper.editUser(editUser);
        if (i == 0){
            return ResultFactory.createResult(CodeEnum.FAIL,"修改失败");
        }
        return ResultFactory.createResult(CodeEnum.SUCCESS, "修改成功");
    }

    @Override
    public String getUserInfoByUserId(String userId) {
        UserPO userPO = userMapper.getUserInfo(userId);
        if (userPO == null){
            return ResultFactory.createResult(CodeEnum.FAIL,"获取用户信息失败");
        }

        UserDTO userDTO = UserDTO.builder().userId(userPO.getUserId()).nick(userPO.getNick()).username(userPO.getUsername()).signature(userPO.getSignature()).photoCounts(userPO.getPhotoCounts()).collectionCounts(userPO.getCollectionCounts()).fanCounts(userPO.getFanCounts()).followCounts(userPO.getFollowCounts()).approvalCounts(userPO.getApprovalCounts()).userAvatar(userPO.getUserAvatar()).build();
        return ResultFactory.createResult(CodeEnum.SUCCESS, "获取用户信息成功",userDTO);
    }
}
